//
//  BLMy_SignView.m
//  Byelone
//
//  Created by Qi Liu on 2021/2/9.
//

#import "BLMy_SignView.h"
#import "BLSignCellView.h"
#import "BLMY_SiView.h"


@interface BLMy_SignView ()
@property (weak, nonatomic) IBOutlet UIView *baseView;
@property (weak, nonatomic) IBOutlet UIImageView *baseImageView;
@property (weak, nonatomic) IBOutlet UIButton *getBtn;
@property(nonatomic,copy)NSDictionary * infoDic;
@property(nonatomic,copy)NSArray * awardArray;
@property(nonatomic,strong)NSMutableArray<BLSignCellView *> * cellArray;
@property(nonatomic,assign)NSInteger signedDays;
@end

@implementation BLMy_SignView



+(void)showSignViewWithDic:(NSDictionary *)dic click:(void(^)(void))click{
    BLMy_SignView * signView = [[[NSBundle mainBundle]loadNibNamed:@"BLMy_SignView" owner:nil options:nil]lastObject];
    signView.frame = BL_WINDOW_FRAME;
    signView.infoDic = dic;
    signView.click = click;
    [BL_WINDOW addSubview:signView];
}

-(BLSignCellView *)creatCellWithTag:(int)tag{
    BLSignCellView * cell = [[[NSBundle mainBundle]loadNibNamed:@"BLSignCellView" owner:nil options:nil]lastObject];
    cell.tag = tag;
    return cell;
}
- (IBAction)btnClick:(UIButton *)sender {

    [self disMissView];
}


#define cellW 65
#define cellH  cellW * 1.2
//#define leftGap 19 //最左边间隔
#define rowGap 9  //列间隔

-(void)awakeFromNib{
    [super awakeFromNib];
    self.backgroundColor = BL_COLOR_ALPHA;
    
    _cellArray = [NSMutableArray array];
  
    CGFloat cellY = (self.baseView.height - cellH) / 2;
    
    CGFloat leftGap =  (self.baseView.width - cellW * 4 - rowGap*3)/2;
    
    for (int i = 0; i < 7; i++) {
        
        BLSignCellView * cell = [self creatCellWithTag:i];
        
        int line = i/4;
        
        int row = i%4;
        
        cell.frame = CGRectMake(row * (cellW + rowGap) + leftGap, cellY + line * (cellH +18) , cellW, cellH);
        if (i == 6 ) {
            cell.width = cellW * 2 + rowGap;
            cell.baseImage.image = [UIImage imageNamed:@"my_sign_will_big"];
            cell.awardLabel.hidden = YES;
            cell.awardLabelBig.hidden = NO;
        }
        cell.dayLabel.text = [NSString stringWithFormat:@"第%d天",i+1];
        cell.signBtn.tag = 1000+i;
        [cell.signBtn addTarget:self action:@selector(signClick:) forControlEvents:UIControlEventTouchUpInside];
        [self.baseView addSubview:cell];
        
        [_cellArray addObject:cell];
    }
    
    self.getBtn.layer.cornerRadius = 21.15;
    self.getBtn.layer.masksToBounds = YES;
}

-(void)setInfoDic:(NSDictionary *)infoDic{
    _infoDic = infoDic;
    
    NSArray *  signConfigList = [infoDic objectForKey:@"signConfigList"];
    
    _awardArray = signConfigList;
    
    NSInteger signedDays = [[infoDic objectForKey:@"signedDays"] integerValue];
    
    _signedDays = signedDays;

    NSArray* reversedArray = [[signConfigList reverseObjectEnumerator] allObjects];
    
    [_cellArray enumerateObjectsUsingBlock:^(BLSignCellView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {

        NSDictionary * giftDic = [reversedArray objectAtIndex:idx];
        obj.awardLabel.text = [NSString stringWithFormat:@"珍珠x%@",[giftDic objectForKey:@"giftAmount"]];
        obj.dayLabel.text = [NSString stringWithFormat:@"第%@天",[giftDic objectForKey:@"signDays"]];
        if (idx < signedDays) {
            obj.baseImage.image = [UIImage imageNamed:@"my_signed"];
            obj.awardLabel.textColor = [UIColor whiteColor];
            obj.signBtn.userInteractionEnabled = NO;
        }
        if (idx == _cellArray.count - 1) {
            obj.awardLabel.hidden = YES;
            obj.awardLabelBig.hidden = NO;
        }
    }];

}


-(void)disMissView{
    [UIView animateWithDuration:0.3 animations:^{
        self.alpha = 0;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}


-(void)signClick:(UIButton *)sender{
    NSInteger tag = sender.tag - 999;
//    NSLog(@"点击第%ld天",tag);
//    if (_click) {
//        _click();
//    }
    

}



- (IBAction)getClick:(UIButton *)sender {
  
     NSDictionary * dic = [_awardArray objectAtIndex:(_signedDays + 1)];
     
     [BLHttpTool getHttpRequest:BL_USER_SIGN param:BL_EMPTYDIC finish:^(NSURLSessionDataTask * _Nonnull data, NSDictionary * _Nonnull obj) {
       
         if ([[obj objectForKey:@"code"] integerValue] == 1) {
             [[NSNotificationCenter defaultCenter]postNotificationName:Notification_login object:nil];
             [UIView animateWithDuration:0.2 animations:^{
                 self.alpha = 0;
             } completion:^(BOOL finished) {
                 [self removeFromSuperview];

                 [BLMY_SiView showSiSuccView:dic];
             }];

         }else if ([[obj objectForKey:@"code"] integerValue] == 2){
            
             [self removeFromSuperview];
             
             [HUD showtip:[obj objectForKey:@"msg"]];
             
             
         }
         NSLog(@"%@",obj);
     } failedBlock:^(id  _Nonnull data, NSError * _Nonnull error) {
         
     }];
     
}

-(void)setDataArray:(NSArray *)array{
    
}


@end
